/**
 * This file is part of jCGT (the Java Cooperative Game Theory library).
 *
 * jCGT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jCGT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with jCGT.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package cooperativegametheory.gui;

import au.com.bytecode.opencsv.CSVReader;
import cooperativegametheory.coalitionfunctions.CoalitionFunction;
import cooperativegametheory.coalitionfunctions.VotingGame;
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 *
 * @author Jonas Brekle <jonas.brekle@gmail.com>
 */
public class CFSettingsVoting extends CFSettingsPanel {

    /** Creates new form CFSettingsVoting */
    public CFSettingsVoting() {
        initComponents();
        voteContainer1.setLayout(new BoxLayout(voteContainer1, BoxLayout.Y_AXIS));
        voteContainer1.setAlignmentX(LEFT_ALIGNMENT);
        voteContainer1.setAlignmentY(TOP_ALIGNMENT);
        addFields(getDefaultVote());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        voteContainer1 = new javax.swing.JPanel();
        voteButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();

        javax.swing.GroupLayout voteContainer1Layout = new javax.swing.GroupLayout(voteContainer1);
        voteContainer1.setLayout(voteContainer1Layout);
        voteContainer1Layout.setHorizontalGroup(
            voteContainer1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 161, Short.MAX_VALUE)
        );
        voteContainer1Layout.setVerticalGroup(
            voteContainer1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 195, Short.MAX_VALUE)
        );

        voteButton.setText("load");
        voteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voteButtonActionPerformed(evt);
            }
        });

        addButton.setText("add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(80, Short.MAX_VALUE)
                .addComponent(addButton)
                .addGap(10, 10, 10)
                .addComponent(voteButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(voteContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addButton)
                    .addComponent(voteButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(voteContainer1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void voteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voteButtonActionPerformed
        voteContainer1.removeAll();
        addFields(load());
        validate();
        repaint(); //?
    }//GEN-LAST:event_voteButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_addButtonActionPerformed

    protected void addFields(Map<String,Float> vote){
        for(String key : vote.keySet()){
            voteContainer1.add(new CFSettingsVotingPlayer(key, vote.get(key)), BorderLayout.SOUTH);
        }
    }
    protected Map<String,Float> getDefaultVote(){
        Map<String,Float> vote = new HashMap<String,Float>();
        vote.put("CDU", 0.2F);
        vote.put("SPD", 0.20F);
        vote.put("FDP", 0.10F);
        vote.put("B90", 0.10F);
        vote.put("Linke", 0.20F);
        vote.put("Sonstige", 0.20F);

        return vote;
    }

    protected Map<String,Float> load(){
        Map<String,Float> vote = new HashMap<String,Float>();
        //load
        final JFileChooser fc = new JFileChooser();
        //In response to a button click:
        int returnVal = fc.showOpenDialog(new JFrame());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //... The user selected a file, get it, use it.
            File file = fc.getSelectedFile();
            CSVReader reader;
            try {
                reader = new CSVReader(new FileReader(file), ';');
                String[] nextLine;
                
                while ((nextLine = reader.readNext()) != null && nextLine.length >= 2) {
                    vote.put(nextLine[0], Float.valueOf(nextLine[1]) / 100);
                }
                
            } catch (FileNotFoundException ex) {
                Logger.getLogger(CFSettingsVoting.class.getName()).log(Level.SEVERE, null, ex);
            }
            catch (IOException ex) {
                Logger.getLogger(CFSettingsVoting.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return vote;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton voteButton;
    private javax.swing.JPanel voteContainer1;
    // End of variables declaration//GEN-END:variables

    @Override
    CoalitionFunction getCF() {
        HashMap<Integer, Float> vote = new HashMap<Integer, Float>() ;
        int i = 0;
        for(Component c : voteContainer1.getComponents()){
            vote.put(i, ((CFSettingsVotingPlayer)c).getVote());
            i++;
        }
        return new VotingGame(vote);
    }

}
